<template>
  <el-container>
    <!--查询条件开始-->
    <el-header height="1">
      <el-form ref="queryForm" size="small" :model="parmsValue.query" label-width="160px" class="middle-form">
        <el-row>
          <el-col :span="6">
            <el-form-item prop="fee_adtype" label="费用分类">
              <el-select v-model="parmsValue.query.fee_adtype" :clearable="true">
                <el-option v-for="(item,index) in dict.fee_adtype" :key="index" :value="index" :label="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="fee_name" label="费用名称">
              <el-input v-model="parmsValue.query.fee_name" :clearable="true" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="state" label="是否启用">
              <el-select v-model="parmsValue.query.state" :clearable="true">
                <el-option v-for="(item,index) in dict.state" :key="index" :value="index" :label="item" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-button type="success" size="small" @click="query">查询</el-button>
          <el-button type="info" size="small" @click="resetFields('queryForm')">重置</el-button>
          <el-button type="success" size="small" @click="showDialog('add')">新增</el-button>
        </el-row>
      </el-form>
    </el-header>
    <!--查询条件结束-->
    <!--列表开始-->
    <el-main>
      <table-pagination-slot
        :table-data="table.feeSetting.tableData"
        :total="table.feeSetting.total"
        :page-size="table.feeSetting.limit"
        :now-page="table.feeSetting.nowPage"
        :dict-data="dict"
        @current-change="currentChange"
      >
        <el-table-column fixed="left" label="操作" width="300">
          <template slot-scope="scope">
            <el-button type="text" @click="showDialog('update',scope.row)">修改</el-button>
            <el-button type="text" @click="showDialog('detail',scope.row)">详情</el-button>
            <el-button type="text" @click="deleteFeeSetting(scope.row)">删除</el-button>
            <el-button type="text" :disabled="scope.row.state === 1" @click="enableFeeSetting(scope.row)">启用</el-button>
            <el-button type="text" :disabled="scope.row.state === 0" @click="disableFeeSetting(scope.row)">停用</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="fee_adtype" label="费用分类">
          <template slot-scope="scope">
            {{ scope.row.fee_adtype | dictTable(dict.fee_adtype) }}
          </template>
        </el-table-column>
        <el-table-column prop="fee_name" label="费用名称" />
        <el-table-column prop="debt_account_name" label="科目名称(负债类)" />
        <el-table-column prop="debt_account_code" label="科目代码(负债类)" />
        <el-table-column prop="loss_account_name" label="科目名称(损益类)" />
        <el-table-column prop="loss_account_code" label="科目代码(损益类)" />
        <el-table-column prop="state" label="是否启用">
          <template slot-scope="scope">
            {{ scope.row.state | dictTable(dict.state) }}
          </template>
        </el-table-column>
        <el-table-column prop="note" label="描述" />
      </table-pagination-slot>
    </el-main>
    <!--列表结束-->
    <!--弹窗开始-->
    <el-dialog :title="editModStr" :visible.sync="parmsValue.updateOpen" :before-close="handleClose">
      <el-form ref="updateForm" size="small" :model="parmsValue.update" label-width="160px" :rules="form.rules" class="middle-form">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="fee_adtype" label="费用分类">
              <el-select v-model="parmsValue.update.fee_adtype" :clearable="true" :disabled="detailMod">
                <el-option v-for="(value,key,index) in dict.fee_adtype" :key="index" :value="index+1" :label="value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="fee_name" label="费用名称">
              <el-input v-model="parmsValue.update.fee_name" :clearable="true" :disabled="detailMod" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="debt_account_name" label="科目名称(负债类)">
              <el-input v-model="parmsValue.update.debt_account_name" :clearable="true" :disabled="detailMod" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="debt_account_code" label="科目代码(负债类)">
              <el-input v-model="parmsValue.update.debt_account_code" :clearable="true" :disabled="detailMod" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="loss_account_code" label="科目代码(损益类)">
              <el-input v-model="parmsValue.update.loss_account_code" :clearable="true" :disabled="detailMod" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="loss_account_name" label="科目名称(损益类)">
              <el-input v-model="parmsValue.update.loss_account_name" :clearable="true" :disabled="detailMod" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="note" label="描述">
              <el-input v-model="parmsValue.update.note" :clearable="true" :disabled="detailMod" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="!addMod" prop="state" label="是否启用">
              <el-select v-model="parmsValue.update.state" :clearable="true" :disabled="detailMod||updateMod">
                <el-option v-for="(value,key,index) in dict.state" :key="index" :value="index" :label="value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-button v-if="updateMod" type="success" size="small" @click="update">保存</el-button>
          <el-button v-if="addMod" type="success" size="small" @click="add">保存</el-button>
          <el-button type="info" size="small" @click="closeDialog">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
    <!--弹窗结束-->
  </el-container>
</template>

<script>
  import { formItems } from '@/utils/formItems'
  import * as kk from '@/utils/k-utils'
  import request from '@/utils/request'
  import axios from 'axios'

  export default {
    data() {
      return {
        dict: {
          fee_adtype: {},
          state: {}
        },
        table: {
          feeSetting: {
            tableData: [],
            total: 100,
            limit: 10,
            nowPage: 1,
            exeid: 'M8FeeManage002'
          }
        },
        form: {
          rules: {
            fee_adtype: [{ required: true, message: '费用分类必选', trigger: 'change' }],
            fee_name: [{ required: true, message: '费用名称必填', trigger: 'change' }],
            loss_account_code: [{ required: true, message: '科目代码(损益类)必填', trigger: 'change' }],
            loss_account_name: [{ required: true, message: '科目名称(损益类)必填', trigger: 'change' }],
            debt_account_code: [{ required: true, message: '科目代码(负债类)必填', trigger: 'change' }],
            debt_account_name: [{ required: true, message: '科目名称(负债类)必填', trigger: 'change' }],
            state: [{ required: true, message: '是否启用必选', trigger: 'change' }]
          }
        },
        parmsValue: {
          // 查询字段值
          query: {
            fee_adtype: '',
            fee_name: '',
            state: ''
          },
          // 增删改字段值
          update: {
            fee_adtype: '', fee_name: '', loss_account_code: '', loss_account_name: '', debt_account_code: '', debt_account_name: '', state: '', note: ''
          },
          updateOpen: false,
          editMod: 'update'
        }
      }
    },
    computed: {
      editModStr() {
        switch (this.parmsValue.editMod) {
        case 'update':
          return '修改费用设置'
        case 'add':
          return '新增费用设置'
        case 'detail':
        default:
          return '费用设置详情'
        }
      },
      updateMod() {
        return this.parmsValue.editMod === 'update'
      },
      detailMod() {
        return this.parmsValue.editMod === 'detail'
      },
      addMod() {
        return this.parmsValue.editMod === 'add'
      }
    },
    beforeMount() {
      this.init()
    },
    // 生命周期结束
    methods: {
      init() {
        const dictReq = []
        const dicts = ['t8_fee_adtype', '1yes0no']
        dicts.forEach(key => dictReq.push(request({
          url: 'system/base/dict/' + key + '.json',
          method: 'post',
          loading: true
        })))
        axios.all(dictReq).then(axios.spread((fee_adtype, state) => {
          this.dict['fee_adtype'] = fee_adtype
          this.dict['state'] = state
          this.loadTable()
        }))
      },
      query() {
        this.table.feeSetting.nowPage = 1
        this.loadTable()
      },
      currentChange(index) {
        this.table.feeSetting.nowPage = index
        this.loadTable()
      },
      loadTable() {
        this.table.feeSetting.fee_adtype = this.parmsValue.query.fee_adtype
        this.table.feeSetting.fee_name = this.parmsValue.query.fee_name
        this.table.feeSetting.state = this.parmsValue.query.state
        kk.k_load_table(this, 'feeSetting', formItems.FEE)
      },
      /**
       * 启用
       * @param params
       */
      enableFeeSetting(params) {
        this.$confirm('确认启用费用吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          kk.k_update_exeid(formItems.FEE, 'enableFeeSet', params).then(resp => {
            if (resp.success) {
              this.loadTable()
            }
          })
        }).catch(() => {
        })
      },
      /**
       * 停用
       * @param params
       */
      disableFeeSetting(params) {
        this.$confirm('确认停用费用吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          kk.k_update_exeid(formItems.FEE, 'disableFeeSet', params).then(resp => {
            if (resp.success) {
              this.loadTable()
            }
          })
        }).catch(() => {
        })
      },
      /**
       * 删除
       * @param row
       */
      deleteFeeSetting(row) {

      },
      showDialog(editMod, params = {}) {
        switch (editMod) {
        case 'add':
          this.parmsValue.editMod = 'add'
          this.parmsValue.updateOpen = true
          break
        case 'update':
          this.parmsValue.editMod = 'update'
          this.parmsValue.updateOpen = true
          this.$nextTick(() => {
            Object.assign(this.parmsValue.update, params)
            this.parmsValue.update = JSON.parse(JSON.stringify(this.parmsValue.update))
          })
          break
        case 'detail':
          this.parmsValue.editMod = 'detail'
          this.parmsValue.updateOpen = true
          this.$nextTick(() => {
            Object.assign(this.parmsValue.update, params)
          })
          break
        default:
        }
      },
      handleClose(done) {
        this.resetFields('updateForm')
        done()
      },
      closeDialog() {
        this.parmsValue.updateOpen = false
        this.resetFields('updateForm')
      },
      resetFields(form) {
        this.$refs[form].resetFields()
      },
      update() {
        this.$refs['updateForm'].validate(valid => {
          if (valid) {
            // 组装修改科目参数
            this.$message.warning('开发中')
            this.assemble_params(this.parmsValue.update)
            this.parmsValue.update.t8_fee_info_id = this.parmsValue.update.id// 费用ID
            kk.k_update_exeid(formItems.FEE, 'editFeeSet', this.parmsValue.update).then(resp => {
              if (resp.success) {
                this.closeDialog()
                this.loadTable()
              }
            })
          }
        })
      },
      add() {
        this.$refs['updateForm'].validate(valid => {
          if (valid) {
            // 组装新增科目参数
            this.$message.warning('开发中')
            this.assemble_params(this.parmsValue.update)
            kk.k_update_exeid(formItems.FEE, 'M8FeeManage001', this.parmsValue.update).then(resp => {
              if (resp.success) {
                this.closeDialog()
                this.loadTable()
              }
            })
          }
        })
      },
      // 组装SQL批处理参数（数组）
      assemble_params(params) {
        // 组装新增科目参数
        // 科目代码(科目表)
        const account_code = []
        // 科目名称(科目表)
        const account_name = []
        // 科目名称(科目表)
        const account_type = []
        // 资产负债子项(科目表)
        const zcfz_item = []
        // 资产负债子项(科目表)
        const lr_item = []
        // 科目基础项目(科目池)
        const subject_item = []
        // 原科目代码(科目表、科目池信息表)
        const out_account_code = []
        // 核算方式(会计出账规则表)
        const acct_mod = []
        account_code[0] = params.debt_account_code// 负债科目代码
        account_code[1] = params.loss_account_code// 损益科目代码
        account_name[0] = params.debt_account_name// 负债科目名称
        account_name[1] = params.loss_account_name// 损益科目名称
        account_type[0] = '2'// 会计类型
        account_type[1] = '4'// 会计类型
        zcfz_item[0] = '其他负债'// 资产负债子项
        zcfz_item[1] = '未分配利润'// 资产负债子项
        lr_item[0] = ''//
        lr_item[1] = '其他费用'//
        subject_item[0] = '应付费用'// 科目池中的科目基础项目
        subject_item[1] = '费用支出'// 科目池中的科目基础项目
        out_account_code[0] = params.out_debt_account_code
        out_account_code[1] = params.out_loss_account_code
        acct_mod[0] = '货币型'// 核算方式
        acct_mod[1] = '净值型'// 核算方式
        params.account_code = account_code
        params.account_name = account_name
        params.account_type = account_type
        params.zcfz_item = zcfz_item
        params.lr_item = lr_item
        params.subject_item = subject_item
        params.out_account_code = out_account_code
        params.acct_mod = acct_mod
      }
    }
  }
</script>
